package com.ynny.dao;

import com.ynny.bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class StudentImpl implements StudentDao{
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public List<Student> selectAllStudents() {
        String sql = "select * from students";
        List<Student> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
        return list;
    }

    @Override
    public String insertStudent(String number ,String name, String gender, String clbum, String grade) {
        String  sql = "insert into students(number,name,grade,clbum,gender) values(?,?,?,?,?)";
        int update = jdbcTemplate.update(sql, number,name,grade,clbum,gender);
        return update>0?"添加成功":"添加失败";
    }

    @Override
    public Student selectStudentByNumber(String number) {
        String sql = "select * from students where number = ?";
        Student student = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Student.class), number);
        return student;

    }

    @Override
    public int updateStudentByName() {
        return 0;
    }
}
